/*
 * TCSS 422 Web Spider Project
 * Group Money: Al McKenzie, Michael Pitts, Taylor Zielske
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

import control.data.SearchHopper;

/**
 * This class runs while the Web Spider is searching.  It will update the user as data
 * is collected.  The user may specify in the StartGUI if this gui will run or not.
 * 
 * @author Taylor Zielske
 * @version November 7, 2011
 */
public class RunningGUI implements Observer{
	/**
	 * The frame
	 */
	private JFrame my_frame;
	/**
	 * The panel
	 */
	private JPanel my_panel;
	/**
	 * The scroll pane
	 */
	private JScrollPane scroll;
	/**
	 * The text area
	 */
	private JTextPane output;

	/**
	 * Initializes the running gui
	 */
	public RunningGUI() {
		my_frame = new JFrame("Web Crawler");
		my_panel = new JPanel();
		my_panel.setLayout(new BorderLayout());
		output = new JTextPane();
		scroll = new JScrollPane(output);
	    scroll.setPreferredSize(new Dimension(200,200));
		output.setEditable(false);
		
	}
	/**
	 * Starts the gui which runs while the program is crawling the web.
	 */
	public void start() {
		JPanel title = new JPanel( new BorderLayout());
		JLabel webcrawler = new JLabel("WEB CRAWLER");
		webcrawler.setFont(new Font("SansSerif", Font.BOLD, 14));
		webcrawler.setForeground(Color.DARK_GRAY);
		title.add(webcrawler);
		my_panel.add(title, BorderLayout.NORTH);
		my_panel.add(scroll, BorderLayout.CENTER);
		my_frame.add(my_panel);
	    my_frame.setSize(new Dimension(500,425));
	    my_frame.setTitle("Web Crawler");
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		my_frame.setVisible(true);
		my_frame.setResizable(false);
	}
	
	/**
	 * Updates the gui with new data.
	 * @param the_arg is the updated data to display.  NOT DEFINAT YET.
	 */
	public void update(final Object[] the_arg) {
		final StringBuffer s = new StringBuffer();
			s.append("Parsed: " + the_arg[0] + "\n");
			s.append("Pages Retrieved: " + the_arg[1].toString() + "\n");
			s.append("Average words per page: " + 
					((float)(((Long)the_arg[2])) / ((Integer)the_arg[1])) + "\n");
			s.append("Average URLs per page: " + 
					(((float)(((Integer)the_arg[3])) / ((Integer)the_arg[1]))) + "\n");
			s.append("Keyword\tAve. hits per page\t\tTotal hits\n");
			String[] words = (String[]) the_arg[6];
			int[] freq = (int[])the_arg[7];
			for (int i = 0; i < words.length; i++) {
				s.append(words[i]);
				s.append("\t");
				//if (words[i].length() < 7) {
					//s.append("\t");
				//}
				s.append((double)Math.round(((float) (freq[i]) / (Integer)the_arg[1])* 100000)/100000 + 
						"\t\t\t" + freq[i] + "\n");
			}
			s.append("\nPage limit: " + (Integer)the_arg[5] + "\n");
			s.append("Average parse time per page " + 
					(System.currentTimeMillis() - (Long)the_arg[4]) / (Integer)the_arg[1] + " msec\n");
			s.append("Total running time: " + 
					(float) (System.currentTimeMillis() - (Long)the_arg[4]) / 1000 + " sec\n\n\n");
			output.setText(s.toString());
		    //output.repaint();
		    //output.revalidate();
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					JScrollBar scrollBar = scroll.getVerticalScrollBar();
					scrollBar.setValue(scrollBar.getMaximum());
				}
			});
		    
	}

	/**
	 * Is called when the SearchHopper has gotten data on a new web site.
	 * @param the_observable should be the SearchHopper, will ignore if not.
	 * @param the_arg
	 */
	@Override public void update(final Observable the_observable, final Object the_arg) {
		if (SearchHopper.class.isInstance(the_observable)) {
			if(Object[].class.isInstance(the_arg)){
				update((Object[]) the_arg);
			}
		}
	}

	/**
	 * Stops the GUI.
	 */
	public void stop() {
		this.my_frame.dispose();		
	}

}
